﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AzureToolkit.Engine
{

    public static class Cron
    {
        public static CronBuilder Hour(int hour)
        {
            return Hour(hour.ToString());
        }

        public static CronBuilder Hour(string hour)
        {
            var builder = new CronBuilder();
            return builder.Hour(hour);
        }

        public static CronBuilder Minute(int minute)
        {
            return Minute(minute.ToString());
        }


        public static CronBuilder Minute(string minute)
        {
            var builder = new CronBuilder();
            return builder.Minute(minute);
        }
    }

    public class CronBuilder
    {
        private const string format = "{0} {1} {2} {3} {4}";
        private string[] parts;

        internal CronBuilder()
        {
            parts = new string[] { "*", "*", "*", "*", "*" };
        }

        public CronBuilder Hour(int hour)
        {
            return Hour(hour.ToString());
        }

        public CronBuilder Hour(string hour)
        {
            parts[1] = hour;

            // If the minute has not been set
            // set the minute to 0. It would be assumed that if
            // the user set hour, they want the job to run once on
            // the hour, not every second durrent that hour
            if (parts[0] == "*")
            {
                parts[0] = "0";
            }

            return this;
        }

        public CronBuilder Minute(int minute)
        {
            return Minute(minute.ToString());
        }

        public CronBuilder Minute(string minute)
        {
            parts[0] = minute;

            return this;
        }

        public override string ToString()
        {
            return String.Format(format, parts);
        }

    }
}
